<!-- 修改密码 -->
<template>
  <div class="modifypaw">
    <div class="pws">
      <p class="modif-pws">{{name}}密码修改</p>
      <el-steps :active="active" align-center finish-status="success">
        <el-step title="确认信息"></el-step>
        <el-step title="输入新密码"></el-step>
        <el-step title="修改成功"></el-step>
      </el-steps>
      <!-- 表单验证 -->
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="旧密码" prop="pawd">
          <el-input type="password" v-model.number="ruleForm.pawd"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm()">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  components: {},
  props: {},
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("旧密码不能为空"));
      }
      setTimeout(() => {
        if (/[\u4E00-\u9FA5]/g.test(value)) {
          callback(new Error("密码不能为中文"));
        } else {
          callback();
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      this.active = 2;
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      active: 1,
      ruleForm: {
        pass: "",
        checkPass: "",
        pawd: ""
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        pawd: [{ validator: checkAge, trigger: "blur" }]
      }
    };
  },
  watch: {},
  computed: {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.active = 3;
          var pawd = {
            username: this.$route.query.name,
            oldPass: this.ruleForm.pawd, // 旧密码
            newPass: this.ruleForm.checkPass // 新密码
          };
          this.$store
            .dispatch("Changepassword", pawd)
            .then(data => {
              this.$message({
                message: "密码修改成功",
                type: "success"
              });
              this.$store.dispatch("LogOut").then(() => location.reload());
            })
            .catch(error => {
              console.log(error);
            });
        } else {
          this.$message({
            message: "密码修改失败",
            type: "warning"
          });
          console.log("error submit!");
          return false;
        }
      });
    },
    resetForm() {
      this.$router.replace({ name: "permission" });
    }
  },
  created() {
    this.name = this.$route.query.name;
  },
  mounted() {}
};
</script>
<style scoped>
.pws {
  width: 350px;
  height: 300px;
  padding: 50px;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-left: -200px;
  margin-top: -200px;
  z-index: 1000;
}
.demo-ruleForm {
  position: absolute;
  bottom: 30px;
}
.modif-pws {
  font-size: 12px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 15px;
}
</style>